package com.yf.tool;

import cn.hutool.core.bean.BeanUtil;
import com.yf.mapper.system.SysUserMapper;
import com.yf.model.system.entity.SysUser;
import com.yf.model.system.enums.GenderEnum;
import com.yf.tool.context.ToolBaseContext;
import com.yf.tool.model.dto.UserInfoDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.stereotype.Component;

import java.util.function.BiFunction;
import java.util.function.Function;


/**
 * UserIdentityInfo Tool
 *
 * @author : YiFei
 * @since : 2025/7/5 15:36
 */
@Component
@RequiredArgsConstructor
public class UserIdentityInfoTool implements BiFunction<Void, ToolContext, UserInfoDTO> {

    private final SysUserMapper userMapper;

    /**
     * 获取用户基础信息
     *
     * @param toolContext the function argument
     * @return UserInfoDTO 对象，包含用户名、昵称、性别、角色、邮箱和电话号码等信息
     */
    @Override
    public UserInfoDTO apply(Void unused, ToolContext toolContext) {
        ToolBaseContext toolContextDTO = BeanUtil.toBean(toolContext.getContext(), ToolBaseContext.class);

        SysUser user = userMapper.selectById(toolContextDTO.getUserId());
        String genderLabel = GenderEnum.MALE.getValue().equals(user.getGender())
                ? GenderEnum.MALE.getLabel()
                : GenderEnum.FEMALE.getLabel();

        return UserInfoDTO.builder()
                .username(user.getUsername())
                .nickname(user.getNickname())
                .gender(genderLabel)
                .roles(toolContextDTO.getRoles())
                .email(user.getEmail())
                .phoneNumber(user.getPhoneNumber())
                .build();
    }
}
